﻿package com.dykj.apps.web.sysmanager.dao;

import java.util.List;

import com.dykj.platform.framework.dao.AbstractPublicDAO;
import com.dykj.platform.framework.exception.DatabaseException;

public class FunctionDAOImpl extends AbstractPublicDAO implements IFunctionDAO {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 *  取得树形功能菜单
	 */
	public List findFunctionById(String parentid) throws DatabaseException {
		StringBuffer hql = new StringBuffer("from SysFunction s where");
		if(parentid==null||"".equals(parentid)){
			hql.append(" s.parentid is null order by s.orderno");
		}else{
			hql.append(" s.parentid = '"+parentid+"' order by s.orderno");
		}
		List list = super.query(hql.toString());
		return list;
	}
	/**
	 * 管理员取得节点
	 */
	public List findFunctionForAdmin(String parentid, String type) throws DatabaseException {
		StringBuffer hql = new StringBuffer("from SysFunction s where 1=1");
		if(parentid==null||"".equals(parentid)){
			hql.append(" and s.parentid is null");
		}else{
			hql.append(" and s.parentid = '"+parentid+"'");
		}
		if("0".equals(type)){
			hql.append(" and s.ifsys != '0'");
		}else if("1".equals(type)){
			hql.append(" and s.ifsys != '0' and s.ifsys != '1'");
		}
		hql.append(" order by s.orderno");
		List list = super.query(hql.toString());
		return list;
	}
	/**
	 * 普通用户取得节点
	 */
	public List findFunctionByRole(String parentid,String roleid) throws DatabaseException {
		StringBuffer hql = new StringBuffer("select s from SysFunction s,SysAcl a where s.functionid=a.functionid and roleid='"+roleid+"'");
		if(parentid==null||"".equals(parentid)){
			hql.append(" and s.parentid is null order by s.orderno");
		}else{
			hql.append(" and s.parentid = '"+parentid+"' order by s.orderno");
		}
		List list = super.query(hql.toString());
		return list;
	}
	/**
	 * 取得子节点的个数
	 */
	public int getFunctionCout(String parentid,String type)throws DatabaseException {
		StringBuffer hql = new StringBuffer("select count(*) from SysFunction s where 1=1");
		if(null!=type){
			hql.append(" and s.type = '");
			hql.append(type);
			hql.append("'");
		}
		if(parentid==null||"".equals(parentid)){
			hql.append(" and s.parentid is null order by s.orderno");
		}else{
			hql.append(" and s.parentid = '"+parentid+"' order by s.orderno");
		}
		List list = super.query(hql.toString());
		if(list.size()>0)
			return Integer.parseInt(list.get(0).toString());
		else
			return 0;
	}
/**
 * 
 * @param roleid  被授权的角色
 * @param croleid 当前角色
 * @return
 * @throws DatabaseException
 */
	public List findAllFunctionId(String roleid,String croleid) throws DatabaseException {
		String sql="";
		if(croleid==null||"".equals(croleid)){
			sql = "select s.functionid,s.title,s.parentid,s.href,a.checked from SysFunction s left join SysAcl a on s.functionid=a.functionid and a.roleid = '"+roleid+"' where s.ifsys='0' order by s.orderno";	
		}else{
			sql = "select b.*,ss.title,ss.parentid,ss.href from SysFunction ss," +
					"(select s.functionid,a.checked from SysAcl s left join SysAcl a on s.functionid=a.functionid and a.roleid = '"+roleid+"' where s.roleid = '"+croleid+"' ) b" +
					" where ss.functionid = b.functionid and ss.ifsys='0' order by ss.orderno";
		}
		List list = super.queryJDBC(sql);
		return list;
	}

}
